﻿using System;
using System.Windows.Forms;
using CefSharp.WinForms;
using CefSharp;
using System.Threading;
using Newtonsoft.Json;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Reflection;
namespace warehouse
{
    public partial class Index : Form
    {
        private ChromiumWebBrowser chrome;
        private string url;
        private int width;
        private int height;
        private static log4net.ILog log = log4net.LogManager.GetLogger("Index");
        public Index(string url, string width, string height)
        {
            this.width = int.Parse(width);
            this.height = int.Parse(height);
            this.url = "".Equals(url) ? "http://www.baidu.com" : url;
            InitializeComponent();
        }

        private void Index_Load(object sender, EventArgs e)
        { 

            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;    //最大化窗体
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            Browser browser = new Browser();
            //\\view\\index.html
            //Application.StartupPath + "\\" + 
            if (!url.StartsWith("http"))
            {
                url = Application.StartupPath + "\\" + url;
            }
            browser.Start(url);
            chrome = browser.getBrowser();
            this.Controls.Add(chrome);
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;//新cefsharp绑定需要优先申明
           string jsonPath= "plugins/devices.json";
            RegisterReadjson(jsonPath,chrome);
            chrome.RegisterJsObject("updater", new UpdaterEvent(chrome), new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false });
            chrome.FrameLoadEnd += Chrome_FrameLoadEnd;

        }
        public static void RegisterReadjson(string jsonPath, ChromiumWebBrowser chrome)
        {
            log.Info(jsonPath);
            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonPath))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    foreach (var item in o)
                    {
                        JObject o2 = (JObject)JsonConvert.DeserializeObject(item.Value.ToString());
                        string className=o2["class"].ToString();
                        string dllPath = o2["dll"].ToString();
                        Console.WriteLine(item.Key.ToString().ToLower());
                        try
                        {
                            Assembly assembly = Assembly.LoadFrom(dllPath);
                            Type type = assembly.GetType(className);
                            chrome.RegisterJsObject(item.Key.ToString().ToLower(), Activator.CreateInstance(type, chrome), new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false }); 
                        }
                        catch(Exception e)
                        {
                            log.Error(e.ToString());
                        }
                            
                    }
                }
            }
        }

        private void Chrome_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            //string jsonPath = "plugins/devices.json";
           // RegisterReadjson(jsonPath, chrome);
            //mounse.AutoClick();
            // string url = "http://172.16.8.67:8090/version.xml";
            //  warehouse.updater.check(url);


        }
    }
}
